package fm.dongman.animefm.view.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import fm.dongman.animefm.R;

/**
 * 私信界面聊天对话框
 * Created by xuyougen on 2017/12/1.
 */

public class ChatDialog extends Dialog {
    private ChatDialog(@NonNull Context context) {
        this(context, R.style.CustomDialogNoDim);
    }

    private ChatDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getWindow() != null) {
            WindowManager.LayoutParams prams = getWindow().getAttributes();
            prams.width = WindowManager.LayoutParams.MATCH_PARENT;
            prams.gravity = Gravity.BOTTOM;
            getWindow().setAttributes(prams);
        }
    }

    public static class Builder {
        private Context mContext;
        private DialogListener mListener;
        private String mInputStr;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setDialogListener(DialogListener listener) {
            mListener = listener;
            return this;
        }

        public Builder setText(String text) {
            mInputStr = text;
            return this;
        }

        public ChatDialog Build() {
            final ChatDialog dialog = new ChatDialog(mContext);
            View layout = View.inflate(mContext, R.layout.dialog_chat_window, null);
            final EditText editText = (EditText) layout.findViewById(R.id.mEtChat);
            final Button send = (Button) layout.findViewById(R.id.mBtnChatSend);
            dialog.setOnShowListener(new OnShowListener() {
                @Override
                public void onShow(DialogInterface dialog) {
                    InputMethodManager manager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    manager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
                }
            });
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if (mListener == null)
                        return;
                    mListener.onTextChanged(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {
                }
            });
            send.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener == null)
                        return;
                    dialog.dismiss();
                    mListener.onSend(send.getText().toString());
                }
            });
            if (mInputStr != null) {
                editText.setText(mInputStr);
            }
            dialog.setContentView(layout);
            return dialog;
        }

        public interface DialogListener {
            void onTextChanged(String text);

            void onSend(String text);
        }
    }
}
